package com.zzyy.study.myconsume;

import cn.hutool.json.JSONUtil;
import com.zzyy.study.config.RabbitMQConfiguration;
import com.zzyy.study.dto.TransferMsg;
import com.zzyy.study.mq.consumer.AbstractIdempotentConsumer;
import com.zzyy.study.mq.dto.Msg;
import com.zzyy.study.service.IAccountService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @auther zzyy
 * @create 2024-10-20 16:08
 */

@Component
@Slf4j
public class TransferMsgConsumer extends AbstractIdempotentConsumer<TransferMsg, Msg<TransferMsg>>
{

    @Resource
    private IAccountService accountService;

    @RabbitListener(queues = RabbitMQConfiguration.Transfer.QUEUE)
    public void dispose(Message message) {
        super.dispose(message);
    }

    @Override
    protected void idempotentConsume(TransferMsg transferMsg) {
        log.info("处理转账消息：{}", JSONUtil.toJsonStr(transferMsg));

        this.accountService.balanceAdd(transferMsg.getToAccountId(),transferMsg.getTransferPrice());

        //估计添加代码，模拟消费失败重试机制
        //int age = 10/0;
    }


}
